var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  Handler.robotTimer = 0
  Handler.robotFlag = false
  Handler.robotInfo = {
  	"table" : [0,0,0,0,0,0,0,0,0,0],
  	"time" : 5000,
  }
}

var handler = Handler.prototype
var local = {}


//设置自动机器人状态
handler.setRobotState = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			if(msg.state === true){
				Handler.robotFlag = true
				Handler.robotInfo.table = msg.table
				Handler.robotInfo.time = msg.time
				if(typeof(Handler.robotInfo.time) !== "number" || Handler.robotInfo.time < 5000){
					Handler.robotInfo.time = 5000
				}
				local.beginRobot()
			}else{
				Handler.robotFlag = false
			}
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
	next(null,{flag : true})
}
//开启机器人自动抽奖
local.beginRobot = function() {
	clearTimeout(Handler.robotTimer)
	if(Handler.robotFlag == false){
		return
	}
	var index = local.getRandomIndex(Handler.robotInfo.table)
	Handler.app.rpc.db.remote.addRobotLottery(null,index,function(data){
	})
	var randomTime = Handler.robotInfo.time *  (Math.random() * 1.5 + 0.5)
	Handler.robotTimer = setTimeout(local.beginRobot,randomTime)
}



//设置库存表
handler.setLotteryInventoryItem = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.setLotteryInventoryItem(session,msg.data,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}

//设置转盘表
handler.setLotteryTable = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.setLotteryTable(session,msg.data,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取抽奖次数列表
handler.getLotteryCountList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.getLotteryCountList(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//设置奖品状态,0 : 未发送   10:已发送完成
handler.sendAwardState = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var targetUid = msg.uid
	var orderNumber = msg.orderNumber
	var state = msg.state
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.sendAwardState(session,targetUid,orderNumber,state,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}

//分享领取奖励
handler.shareAward = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!!uid){
	  	self.app.rpc.db.remote.getPlayerObject(session,uid,"refreshList",function(data) {
	  		if(data.shareCount >= 1){
	  			next(null,{flag : false})
	  			return
	  		}
	  		//领取抽奖次数
	  		data.shareCount++
	  		self.app.rpc.db.remote.setPlayerObject(session,uid,"refreshList",data,function() {})
			self.app.rpc.db.remote.addLotteryCount(session,uid,1,function(){})
	  	})
	}else{
		next(null,{flag : false})
	}
}

//抽奖
handler.lottery = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		self.app.rpc.db.remote.lottery(session,uid,function(flag){
			next(null,{flag : flag})
		})
	}else{
		next(null,{flag : false})
	}
}
//获取抽奖次数
handler.getLotteryCount = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.remote.getLotteryCount(session,uid,function(data){
		next(null,{data : data})
	})
}
//增加抽奖次数
handler.addLotteryCount = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var targetUid = msg.uid
	var count = msg.count
	if(typeof(targetUid) !== "number" || typeof(count) !== "number"){
		cb(flase)
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.addLotteryCount(session,targetUid,count,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取奖励表
handler.getLotteryTable = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.remote.getLotteryTable(session,function(data){
		next(null,{data : data})
	})
}
//获取单个玩家奖品背包
handler.getMyLotteryAward = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.remote.getLotteryAwardByUid(session,uid,function(data){
		next(null,{data : data})
	})	
}
//获取已获得奖品列表
handler.getLotteryAwardList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.getLotteryAwardList(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//添加机器人数据
handler.addRobotLottery = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.addRobotLottery(session,msg.index,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}

//获取重要物品记录
handler.getImportanceAwardList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.remote.getImportanceAwardList(session,function(data){
		next(null,{data : data})
	})
}

//获取奖品库存
handler.getLotteryInventory = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.getLotteryInventory(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取抽奖记录
handler.getLotteryHistory = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.remote.getLotteryHistory(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}

local.getDateString = function() {
	var myDate = new Date()
	var month = myDate.getMonth() + 1
	var date = myDate.getDate()
	if(month < 10){
		month = "0"+month
	}
	if(date < 10){
		date = "0"+date
	}
	var dateString = parseInt(""+myDate.getFullYear() + month + date)
	return dateString
}

local.getRandomIndex = function(table) {
	var weightAll = 0
	for(var i in table){
		weightAll += table[i]
	}
	//获取奖励
	var tmpRand = Math.random() * weightAll
	var weightCur = 0
	for(var i in table){
		if(tmpRand >= weightCur && tmpRand < table[i] + weightCur){
			return parseInt(i)
		}else{
			weightCur += table[i]
		}
	}
}